class TreeNode:
    def __init__(self, val=0, left=None, right=None):
        self.val = val
        self.left = left
        self.right = right
from typing import *
class Solution:
    def leafSimilar(self, root1: Optional[TreeNode], root2: Optional[TreeNode]) -> bool:

        l = []
        r = []
        def f(root:TreeNode,l):
            if root is None:
                return
            if root.left is None and root.right is None:
                l.append(root.val)
            
            f(root.left,l)
            f(root.right,l)
        f(root1,l)
        f(root2,r)
        return l == r